package com.exigen.performance.test.webapp.dto;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.exigen.performance.test.webapp.domain.DashboardEntity;
import com.exigen.performance.test.webapp.domain.ProjectEntity;
import com.exigen.performance.test.webapp.services.DashboardService;

public class DashboardDto {

	// List of dash board elements that should be displayed in data grid
	List<DashboardEntity> dashboards;
	// List of project available to add on dash board
	List<ProjectEntity> newProjects;
	// Project to add on dash board
	ProjectEntity selectedNewProject;
	// Project to delete from dash board
	DashboardEntity selectedForDeleteDashboard;

	DashboardService dashboardServices;

	/**
	 * Method that will be launch at page load to populate data grid with
	 * projects and related data.
	 */
	public void onPageLoad() {
		this.getProjectList();
	}

	/**
	 * Method to populate data grid with dash board elements.
	 */
	public void getProjectList() {
		try {
			this.dashboards = dashboardServices.getDashboardList();
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", e
							.getMessage()));
		}
	}

	/**
	 * Method to add new project to dash board
	 */
	public void addDashboardElement() {
		DashboardEntity newElement = new DashboardEntity();
		newElement.setProjectId(selectedNewProject.getId());
		try {
			dashboardServices.createNewDashboardElement(newElement);
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", e
							.getMessage()));
		}

	}

	/**
	 * Method to return list of not used in dash board projects.
	 */
	public void getNewProjectList() {
		try {
			newProjects = dashboardServices
					.getAvailableProjectListForDashbord();
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", e
							.getMessage()));
		}

	}

	/**
	 * Method to update dash board data.
	 * 
	 * @param event
	 */
	public void updateDashboard(ActionEvent event) {
		try {
			dashboardServices.updateDashboard();
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!",
							"Dashboard was updated."));
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", e
							.getMessage()));

		}

	}

	// Getters&Setters
	public DashboardService getDashboardServices() {
		return dashboardServices;
	}

	public void setDashboardServices(DashboardService dashboardServices) {
		this.dashboardServices = dashboardServices;
	}

	public List<ProjectEntity> getNewProjects() {
		return newProjects;
	}

	public void setNewProjects(List<ProjectEntity> newProjects) {
		this.newProjects = newProjects;
	}

	public ProjectEntity getSelectedNewProject() {
		return selectedNewProject;
	}

	public void setSelectedNewProject(ProjectEntity selectedNewProject) {
		this.selectedNewProject = selectedNewProject;
	}

	public List<DashboardEntity> getDashboards() {
		return dashboards;
	}

	public void setDashboards(List<DashboardEntity> dashboards) {
		this.dashboards = dashboards;
	}

	public DashboardEntity getSelectedForDeleteDashboard() {
		return selectedForDeleteDashboard;
	}

	// When dashboard element is set, service called to delete it.
	public void setSelectedForDeleteDashboard(
			DashboardEntity selectedForDeleteDashboard) {
		this.selectedForDeleteDashboard = selectedForDeleteDashboard;
		this.dashboardServices
				.deleteDashboardEntity(this.selectedForDeleteDashboard);
		this.getProjectList();
		selectedForDeleteDashboard = null;

	}
}
